package toDo.app.controllers;

import java.util.Calendar;

import toDo.app.R;
import toDo.app.models.TimeAuthority;
import toDo.app.models.ToDoListItem;
import database.DatabaseFacade;
import interfaces.ICreateToDoActivity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.DatePicker;

public class CreateToDoActivityClickController implements OnClickListener, OnDateSetListener {
	private ICreateToDoActivity activity;
	private Calendar dateAndTime = Calendar.getInstance();

	public CreateToDoActivityClickController(ICreateToDoActivity activity) {
		this.activity = activity;
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()) {
			case R.id.createToDoDateField:
				processDateEvent();
				break;
			case R.id.createToDoAddButton:
				processAddEvent();
				break;
			case R.id.createToDoBackButton:
				processBackEvent();
				break;
		}
	}

	public void processDateEvent() {
		new DatePickerDialog(activity, this, dateAndTime.get(Calendar.YEAR), dateAndTime.get(Calendar.MONTH),
				dateAndTime.get(Calendar.DAY_OF_MONTH)).show();
	}

	public void processAddEvent() {
		// public ToDoListItem(String name, String description, String
		// comments, int time, String type, int _id, String location,
		// int completed{
		ToDoListItem item = new ToDoListItem(activity.getName(), activity.getDescription(), activity.getComments(),
				activity.getDate(), activity.getCategory(), activity.getId(), activity.getLocation(),
				activity.getCompleted());
		DatabaseFacade.processToDoListItem(activity, activity.getUser(), item);
		activity.setResult(ICreateToDoActivity.RESULT_OK);
		activity.finish();
	}

	public void processBackEvent() {
		activity.setResult(ICreateToDoActivity.RESULT_CANCELED);
		activity.finish();
	}

	@Override
	public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
		activity.setDateText(TimeAuthority.componentTimeToString(year, monthOfYear, dayOfMonth));
		activity.setDate(TimeAuthority.componentTimeToTimestamp(year, monthOfYear, dayOfMonth, 0, 0, 0));
	}
}
